//
//  DHRoutine.m
//  DummyHTTPS
//
//  Created by Mike on 1/21/07.
//

#import "DHRoutine.h"

@interface DHRoutine (Private)

- (NSMutableData *)receivedData;
- (void)setReceivedData:(NSMutableData *)newReceivedData;
- (id)target;
- (void)setTarget:(id)newTarget;
- (SEL)selector;
- (void)setSelector:(SEL)newSelector;
- (NSURL *)URL;
- (void)setURL:(NSURL *)newURL;
- (NSURLRequest *)request;
- (void)setRequest:(NSURLRequest *)newRequest;
- (NSURLConnection *)connection;
- (void)setConnection:(NSURLConnection *)newConnection;
- (NSMutableDictionary *)context;
- (void)setContext:(NSMutableDictionary *)newContext;

@end

@implementation DHRoutine (Private)
- (NSMutableData *)receivedData
{
	return receivedData;
}

- (void)setReceivedData:(NSMutableData *)newReceivedData
{
	id oldReceivedData = nil;
	if (newReceivedData != oldReceivedData) {
		oldReceivedData = receivedData;
		receivedData = [newReceivedData retain];
		[oldReceivedData autorelease];
	}
}

- (id)target
{
	return target;
}

- (void)setTarget:(id)newTarget
{
	target = newTarget;
}

- (SEL)selector
{
	return selector;
}

- (void)setSelector:(SEL)newSelector
{
	selector = newSelector;
}

- (NSURL *)URL
{
	return URL;
}

- (void)setURL:(NSURL *)newURL
{
	id oldURL = nil;
	if (newURL != oldURL) {
		oldURL = URL;
		URL = [newURL retain];
		[oldURL autorelease];
	}
}

- (NSURLRequest *)request
{
	return request;
}

- (void)setRequest:(NSURLRequest *)newRequest;
{
	id oldRequest = nil;
	if (newRequest != oldRequest) {
		oldRequest = request;
		request = [newRequest retain];
		[oldRequest autorelease];
	}
}

- (NSURLConnection *)connection
{
	return connection;
}

- (void)setConnection:(NSURLConnection *)newConnection
{
	id oldConnection = nil;
	if (newConnection != oldConnection) {
		oldConnection = connection;
		connection = [newConnection retain];
		[oldConnection autorelease];
	}
}

- (NSMutableDictionary *)context
{
	return context;
}

- (void)setContext:(NSMutableDictionary *)newContext
{
	id oldContext = nil;
	if (newContext != oldContext) {
		oldContext = context;
		context = [newContext retain];
		[oldContext autorelease];
	}
}

@end


@implementation DHRoutine

- init
{
    if ((self = [super init])) {
		NSLog(@"< URL utility generated: %@ >", self);
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)loadURLfromString:(NSString *)aURLstring target:(id)aTarget selector:(SEL)aSelector {
	[self setReceivedData:[NSMutableData data]];										// setup new data
	[self setTarget:aTarget];															// remember the target
	[self setSelector:aSelector];														// remember the selector
	[self setURL:[NSURL URLWithString:aURLstring]];										// remember the URL
	[self setRequest:[NSURLRequest requestWithURL:[self URL]
			cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:11.]];		// remember the request
	[self setContext:[NSMutableDictionary dictionary]];									// setup new context

	if ([self request]) {
		[self setConnection:[NSURLConnection connectionWithRequest:[self request] delegate:self]];	// do it, damnit!
	}
}

- (void)cancelLoad {
	if ([self connection] != nil) {
		[[self connection] cancel];
	}
	[self setConnection:nil];
	NSLog(@"*** Connection Killed ***");
}

// NSURLConnection delegate methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	[[self context] setObject:response forKey:@"response"];
	[[self receivedData] setLength:0];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)theRequest redirectResponse:(NSURLResponse *)redirectResponse
{
	return [[theRequest retain] autorelease];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	[[self context] setObject:error forKey:@"error"];
	[target performSelector:selector withObject:nil withObject:[[[self context] retain] autorelease]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	[target performSelector:selector withObject:[[[self receivedData] retain] autorelease] withObject:[[[self context] retain] autorelease]];
}

@end
